﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScrumMonster;
using System.Collections.ObjectModel;
using System.IO;

namespace ScrumMonsterForm
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnGenerateCsvFile_Click(object sender, EventArgs e)
        {
            FogBugzContext context = new FogBugzContext(txtUrl.Text, txtUsername.Text, txtPassword.Text);

            if (!context.Logged)
            {
                MessageBox.Show("Authentication Failed. Wrong username or password.");
                return;
            }

            ReadOnlyCollection<Task> tasks = context.GetTasks(txtProjectName.Text, txtFixfor.Text);

            GenerateFile(txtFilePath.Text, tasks.ToList());
        }

        private void GenerateFile(string path, List<Task> tasks)
        {
            StreamWriter writer = System.IO.File.CreateText(path);
                        
            foreach (Task task in tasks)
            {
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", task.Number, task.Title, task.EmailAssignedTo, task.Status, task.EstimatedTime.TotalHours, task.ElapsedTime.TotalHours));
            }

            writer.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = saveFileDialog.FileName;
            }

        }
    }
}
